package uk.ac.aber.Blockmation.ColorsTable;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import javax.swing.SwingUtilities;

import uk.ac.aber.Blockmation.NewProjectDialog;
import uk.ac.aber.Blockmation.MainFrame;


/**
 * JTable with rows and columns filled with colorful squares to serve
 * as a palette of colors. Data of each cell is of class Color.
 * It's using OurColorsTableMode and TableColorRenderer class as 
 * its model to fill in each cell's background with specific color.
 * Table properties are changed in the constructor to ensure it 
 * behaves like a color palette, 
 * not like an ordinary table. (Such as: header's turned off, only 
 * selection of cells allowed, etc.) 
 * 
 * < /br>
 * <em> Related to MainFrame class which is not very OO concept - 
 * justified by this being a learning curve. However, you can pass null
 * to use this class independently also, so not too bad :). <em>
 * @author Aron
 *
 */
public class ColorsTable extends JTable
{
    private MainFrame mainFrame;

	
    public ColorsTable(MainFrame mainFrame) {
        super(new OurColorsTableModel());
        this.mainFrame = mainFrame;
 
        this.setPreferredScrollableViewportSize(new Dimension(500, 70));
        //this.setFillsViewportHeight(true);
        this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        this.setColumnSelectionAllowed(false);
        this.setRowSelectionAllowed(false);
        this.setRowHeight(16);
        
        // Set the first visible column to 100 pixels wide
        int columns = this.getColumnModel().getColumnCount();
        int width = 22;
        for (int i=0; i < columns; i++)
        {
        	TableColumn col = this.getColumnModel().getColumn(i);
            col.setPreferredWidth(width);
        }
        this.setTableHeader(null);



        //Set up renderer and editor for the Favorite Color column.
        this.setDefaultRenderer(Color.class,
                                 new TableColorRenderer());
        //this.setDefaultEditor(Color.class, new ColorEditor());      

    }
    
    /**
     * Invoked every time user selects a color cell. It then 
     * adjusts the drawing color of DrawingPad object in MainFrame object
     * with the chosen color.
     * 
     */
    public void valueChanged(ListSelectionEvent event) {

        // See if this is a valid this selection
        if( event.getSource() == this.getSelectionModel()
                                        && event.getFirstIndex() >= 0 )
        {
        	if (mainFrame != null)
        	{
                mainFrame.getPad().setColor( getSelectedColor());
        	}

        }

    }
    
    /**
     * Returns the selected color from the table cell.
     * @return selected color
     */
    public Color getSelectedColor()
    {
        TableModel model = (TableModel)this.getModel();

        // Determine the selected item
        Color color = (Color)model.getValueAt(this.getSelectedRow(),
                                                this.getSelectedColumn() );

        // Display the selected item
        System.out.println( "Color selected = " + color );
        //if (color == null) color = Color.black; //default

        return color;
    }
    
}
